﻿using System;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;

using SIMSProject.ControlTemplates.SIMSProject;
using Microsoft.SharePoint.WebControls;
using SIMSProject.Common;
using System.Data;
using Microsoft.SharePoint.Utilities;

namespace SIMSProject.Views.Sale.OrderRequest
{
    public partial class OrderRequestUserControl : UserControl
    {
        
        SPItem item = SPContext.Current.Item;
        SPWeb web = SPContext.Current.Web;

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            OrderProductAdding productAddingControl = (OrderProductAdding)Page.LoadControl("~/_CONTROLTEMPLATES/SIMSProject/OrderProductAdding.ascx");
            productAddingControl.ParentListName = "Order";
            productAddingControl.SetSessionName(this.hdfSessionName.Value);
            if (SPContext.Current.FormContext.FormMode == SPControlMode.Display || (SPContext.Current.FormContext.FormMode == SPControlMode.Edit && item["Status"].ToString() != "Initiating") || (SPContext.Current.FormContext.FormMode == SPControlMode.Edit && item["Status"].ToString() == "Initiating" && !web.IsCurrentUserMemberOfGroup(web.Groups[Constant.Groups.SalesDepartment].ID))) productAddingControl.displayMode = "display";
            this.phProductAddingModule.Controls.Add(productAddingControl);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.hdfSessionName.Value = Guid.NewGuid().ToString().Replace("-","");
                this.LoadData();
                this.hdfURL.Value = Request.Url.Host;
                this.hdfFormMode.Value = SPContext.Current.FormContext.FormMode.ToString();
                this.btBackInfo.Attributes.Add("onclick", "if($('#tdComment textarea').val()==''){alert('Please enter comment!');return false;}");
                this.btDirectorReject.Attributes.Add("onclick", "if($('#tdDirectorComment textarea').val()==''){alert('Please enter comment!');return false;};return confirm('Are you sure to reject this order ?')");
                this.btReject.Attributes.Add("onclick", "if($('#tdComment textarea').val()==''){alert('Please enter comment!');return false;};return confirm('Are you sure to reject this order ?')");
            }

            if (SPContext.Current.FormContext.FormMode != SPControlMode.New)
            {
                this.hdfStatus.Value = SPContext.Current.Item["Status"].ToString();
            }
            else
            {
                SPContext.Current.Item["Status"] = "Initiating";
                this.hdfStatus.Value = "Initiating";
            }
            
            ShowHideControlInForm();
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            if (SPContext.Current.FormContext.FormMode == SPControlMode.Edit || SPContext.Current.FormContext.FormMode == SPControlMode.New)
            {
                SPContext.Current.FormContext.OnSaveHandler += this.CustomSaveHandler;
            }
        }

        private void CustomSaveHandler(object o, EventArgs e)
        {
            SaveButton.SaveItem(SPContext.Current, false, string.Empty);
            if (this.hdfStatus.Value=="Initiating")
            {
                UpdateProductDetail();
            }
        }
        
        private void LoadData()
        {
            if (SPContext.Current.FormContext.FormMode == SPControlMode.New)
            {
                this.lblUserField.Text = SPContext.Current.Web.CurrentUser.Name;
                this.lblUserField.Visible = true;
            }
        }

        protected void btApprove_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                SPContext.Current.Item["Director"] = SPContext.Current.Web.CurrentUser;
                SPContext.Current.Item["Approve Date"] = DateTime.Now;
                SaveButton.SaveItem(SPContext.Current, false, string.Empty);

                SPUser created = this.getUser(Constant.Order.CreatedBy);
                SPUser saleManager = this.getUser(Constant.Order.SaleManager);
                string cc = Common.Common.GetEmailAddressesFromName(created.LoginName) + ";" + Common.Common.GetEmailAddressesFromName(saleManager.LoginName);
                SaveButton.SaveItem(SPContext.Current, false, string.Empty);
                
                if (CheckProduct(true))
                {
                    SPContext.Current.Item["Status"] = "Approved";
                    Common.Common.SendEmail(SPContext.Current.Web, Common.Common.GetEmailAddressesFromName("Inventory Manager"), cc, "[Approved Order] " + SPContext.Current.Item[Constant.Order.OrderCode], BuildMailContent());
                }
                else
                {
                    SPContext.Current.Item["Status"] = "On Hold";
                    Common.Common.SendEmail(SPContext.Current.Web, Common.Common.GetEmailAddressesFromName("Inventory Manager"), cc, "[On Hold Order] " + SPContext.Current.Item[Constant.Order.OrderCode], BuildMailContent());
                }
                SaveButton.SaveItem(SPContext.Current, false, string.Empty);
                this.Page.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup();</script>");
            }
        }

        private bool CheckProduct(bool hasAlert)
        {
            DataTable dt = Session[this.hdfSessionName.Value] as DataTable;
            if (dt != null && dt.Rows.Count != 0)
            {
                string message = string.Empty;

                foreach (DataRow dr in dt.Rows)
                {
                    int id = 0;
                    Int32.TryParse(Convert.ToString(dr["id"]), out id);
                    int quantity = 0;
                    Int32.TryParse(Convert.ToString(dr["Quantity"]), out quantity);

                    SPListItem product = SPContext.Current.Web.Lists["Product"].GetItemById(id);

                    int productQuantity = 0;
                    Int32.TryParse(Convert.ToString(product["Quantity"]), out productQuantity);

                    if (quantity > productQuantity)
                    {
                        message += "The quanlity of " + product["Title"] + " is not enough\\r\\n";

                    }
                }
                if (message != string.Empty && hasAlert) 
                {
                    message += "This order will change to On Hold status automaticly!";
                    message = "<script type='text/javascript'>window.alert('" + message + "');</script>";
                    this.Page.Response.Write(message);
                    return false;
                }
                else return true;
            }
            else
            {
                this.Page.Response.Write("<script type='text/javascript'>window.alert('Please insert any Product in this order');</script>");
                return false;
            }
        }

        private void AddNewProductTracking()
        {
                        // Add Product Tracking header
                        SPList list = web.Lists["Product Tracking"];
                        SPListItem newItem = list.Items.Add();
                        newItem["Title"] = "Tracking for Order Number : " + SPContext.Current.Item["Order Code"].ToString();
                        newItem["Tracking Type"] = "Export";
                        newItem["Description"] = "Customer Information:\nCustomer Name:" + SPContext.Current.Item["Customer Name"].ToString() + "\nCustomer Address:" + SPContext.Current.Item["Customer Address"].ToString() + "\nCustomer Phone Number:" + SPContext.Current.Item["Customer Phone Number"].ToString();
                        newItem["Month"] = DateTime.Now.Month;
                        newItem["Year"] = DateTime.Now.Year;
                        newItem.Update();

                        // Add Product tracking detail
                        int id = newItem.ID;
                        int totalQuantity = 0;
                        DataTable ProductTable = (DataTable)Session[this.hdfSessionName.Value];

                        SPList childList = web.Lists["Product Tracking Detail"];

                        for (int i = 0; i < ProductTable.Rows.Count; i++)
                        {
                            SPQuery query = new SPQuery();
                            query.Query = "<Where><And><Eq><FieldRef Name='Title' /><Value Type='Text'>" + id.ToString() + "</Value></Eq><Eq><FieldRef Name='Product_x0020_Id' /><Value Type='Text'>" + Convert.ToString(ProductTable.Rows[i]["id"]) + "</Value></Eq></And></Where>";
                            if (childList.GetItems(query).Count == 0)
                            {
                                SPListItem item = childList.Items.Add();
                                item["Title"] = id;
                                item["Product Name"] = ProductTable.Rows[i]["ProductName"];
                                item["Product Code"] = ProductTable.Rows[i]["ProductCode"];
                                item["Product Id"] = ProductTable.Rows[i]["id"];
                                item["Quantity"] = ProductTable.Rows[i]["Quantity"];
                                item.Update();
                            }
                            else
                            {
                                SPListItem item = childList.GetItems(query)[0];
                                item["Quantity"] = ProductTable.Rows[i]["Quantity"];
                                item.Update();
                            }

                            int temp = 0;
                            Int32.TryParse(Convert.ToString(ProductTable.Rows[i]["Quantity"]), out temp);
                            totalQuantity += temp;
                        }

                        newItem["Total Quantity"] = totalQuantity;
                        newItem.Update();
                        this.UpdateProduct();
                    }

        private void UpdateProduct()
        {
            DataTable dt = Session[this.hdfSessionName.Value] as DataTable;
            int id, quantity;
            SPList listProduct = web.Lists["Product"];
            SPListItem item;
            if (dt != null)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    id = (int)dr["id"];
                    quantity = (int)dr["Quantity"];
                    item = listProduct.GetItemById(id);
                    int currentQuantity = Convert.ToInt32(item["Quantity"]);
                    item["Quantity"] = currentQuantity - quantity;
                    item.Update();
                }
            }
        }

        private void UpdateProductDetail()
        {
            int id = SPContext.Current.Item.ID;
            int totalQuantity = 0;
            int totalPrice = 0;
            DataTable ProductTable = (DataTable)Session[this.hdfSessionName.Value];

            SPList childList = SPContext.Current.Web.Lists["Order Detail"];

            for (int i = 0; i < ProductTable.Rows.Count; i++)
            {
                SPQuery query = new SPQuery();
                query.Query = "<Where><And><Eq><FieldRef Name='Title' /><Value Type='Text'>" + id.ToString() + "</Value></Eq><Eq><FieldRef Name='Product_x0020_Id' /><Value Type='Text'>" + Convert.ToString(ProductTable.Rows[i]["id"]) + "</Value></Eq></And></Where>";
                if (childList.GetItems(query).Count == 0)
                {
                    SPListItem item = childList.Items.Add();
                    item["Title"] = id;
                    item["Product Name"] = ProductTable.Rows[i]["ProductName"];
                    item["Product Code"] = ProductTable.Rows[i]["ProductCode"];
                    item["Product Id"] = ProductTable.Rows[i]["id"];
                    item["Quantity"] = ProductTable.Rows[i]["Quantity"];
                    item["Price"] = ProductTable.Rows[i]["ProductPrice"];
                    item["Type of unit"] = ProductTable.Rows[i]["ProductUnitType"];
                    item.Update();
                }
                else
                {
                    SPListItem item = childList.GetItems(query)[0];
                    item["Quantity"] = ProductTable.Rows[i]["Quantity"];
                    item.Update();
                }

                int temp = 0;
                Int32.TryParse(Convert.ToString(ProductTable.Rows[i]["Quantity"]), out temp);
                totalQuantity += temp;
                totalPrice += GetTotalPrice(Convert.ToInt16(ProductTable.Rows[i]["id"]))* temp;
                SaveButton.SaveItem(SPContext.Current, false, string.Empty);
            }

            SPContext.Current.Item[Constant.Order.TotalQuantity] = totalQuantity;
            SPContext.Current.Item[Constant.Order.TotalPrice] = totalPrice;
        }

        private int GetTotalPrice(int id)
        {
            SPItem item = web.Lists["Product"].GetItemById(id);
            return Convert.ToInt32(item["Price"].ToString());
        }

        protected void btReject_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                SPContext.Current.Item["Status"] = "Rejected";
                SPContext.Current.Item["Processor"] = SPContext.Current.Web.CurrentUser;
                SPContext.Current.Item["Processed Date"] = DateTime.Now;
                SaveButton.SaveItem(SPContext.Current, false, string.Empty);
                
                SPUser created = this.getUser(Constant.Order.CreatedBy);
                Common.Common.SendEmail(SPContext.Current.Web, Common.Common.GetEmailAddressesFromName(created.LoginName), string.Empty, "[SIMS - Order][Rejected Order] " + SPContext.Current.Item[Constant.Order.OrderCode], BuildMailContent());
                    
                this.Page.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup();</script>");
            }
        }

        protected void btDirectorReject_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                SPContext.Current.Item["Status"] = "Rejected";
                SPContext.Current.Item["Director"] = SPContext.Current.Web.CurrentUser;
                SPContext.Current.Item["Approve Date"] = DateTime.Now;
                SaveButton.SaveItem(SPContext.Current, false, string.Empty);
                this.Page.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup();</script>");
                SPUser created = this.getUser(Constant.Order.CreatedBy);
                SPUser saleManager = this.getUser(Constant.Order.SaleManager);
                Common.Common.SendEmail(SPContext.Current.Web, Common.Common.GetEmailAddressesFromName(created.LoginName), Common.Common.GetEmailAddressesFromName(saleManager.LoginName), "[SIMS - Order][Rejected Order] " + SPContext.Current.Item[Constant.Order.OrderCode], BuildMailContent());
                
            }
        }

        protected void btSendToDirector_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                SPContext.Current.Item["Status"] = "Director Review";
                SPContext.Current.Item["Processor"] = SPContext.Current.Web.CurrentUser;
                SPContext.Current.Item["Processed Date"] = DateTime.Now;
                web.AllowUnsafeUpdates = true;
                SaveButton.SaveItem(SPContext.Current, false, string.Empty);
                SPUser created = this.getUser(Constant.Order.CreatedBy);
                Common.Common.SendEmail(SPContext.Current.Web, Common.Common.GetEmailAddressesFromName("Director"), Common.Common.GetEmailAddressesFromName(created.LoginName), "[SIMS - Order][Director Review Order] " + SPContext.Current.Item[Constant.Order.OrderCode], BuildMailContent());
                this.Page.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup();</script>");
            }
        }

        protected SPUser getUser(string field)
        {
            string strUser = SPContext.Current.Item[field].ToString();
            string[] information = strUser.Split(';');
            SPUser user = SPContext.Current.Web.SiteUsers.GetByID(Convert.ToInt32(information[0]));
            return user;
        }

        protected void btBackInfo_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                SPContext.Current.Item["Status"] = "Initiating";
                SPContext.Current.Item["Processor"] = SPContext.Current.Web.CurrentUser;
                SPContext.Current.Item["Processed Date"] = DateTime.Now;
                SaveButton.SaveItem(SPContext.Current, false, string.Empty);
                SPUser user = getUser(Constant.Order.CreatedBy);

                Common.Common.SendEmail(SPContext.Current.Web, Common.Common.GetEmailAddressesFromName(user.LoginName), string.Empty, "[SIMS - Order][Back Information Order] " + SPContext.Current.Item[Constant.Order.OrderCode], BuildMailContent());
                this.Page.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup();</script>");
            }
        }

        protected void btFinish_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                if (CheckProduct(true))
                {
                    SPContext.Current.Item["Status"] = "Finished";
                    SPContext.Current.Item["InventoryManager"] = SPContext.Current.Web.CurrentUser;
                    SPContext.Current.Item["Finished Date"] = DateTime.Now;
                    SaveButton.SaveItem(SPContext.Current, false, string.Empty);
                    AddNewProductTracking();

                    SPUser created = this.getUser(Constant.Order.CreatedBy);
                    SPUser saleManager = this.getUser(Constant.Order.SaleManager);
                    string cc = Common.Common.GetEmailAddressesFromName(created.LoginName) + ";" + Common.Common.GetEmailAddressesFromName(saleManager.LoginName);
                    Common.Common.SendEmail(SPContext.Current.Web, Common.Common.GetEmailAddressesFromName("Director"), cc, "[SIMS - Order][Finished Order] " + SPContext.Current.Item[Constant.Order.OrderCode], BuildMailContent());
                    this.Page.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup();</script>");
                }
                else
                {
                    SPContext.Current.Item["Status"] = "On Hold";
                    SaveButton.SaveItem(SPContext.Current, false, string.Empty);
                    this.Page.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup();</script>");
                }
            }
        }

        protected void btSendOrder_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                DateTime expectedDate = Convert.ToDateTime(dtfExpectedDate.Value);
                if (expectedDate.CompareTo(DateTime.Today) < 0 )
                {
                    dtfExpectedDate.IsValid = false;
                    dtfExpectedDate.ErrorMessage = "Expected Date must set to be in the future";
                    return;
                }

                DataTable dt = Session[this.hdfSessionName.Value] as DataTable;
                if (dt != null && dt.Rows.Count != 0)
                {

                    if (!this.cbCustomer.Value.ToString().Contains("Yes"))
                    {
                        int id  = addNewCustomer();
                        SPContext.Current.Item["cbCustomer"] = "Yes";
                        SPContext.Current.Item["Customer"] = id; 
                    }
                    SPContext.Current.Item["Status"] = "Pending";

                    SaveButton.SaveItem(SPContext.Current, false, string.Empty);
                    this.CustomSaveHandler(null, null);
                    SaveButton.SaveItem(SPContext.Current, false, string.Empty);
                    Common.Common.SendEmail(SPContext.Current.Web, Common.Common.GetEmailAddressesFromName("Sale Manager"), string.Empty, "[SIMS - Order][New Pending Order] " + SPContext.Current.Item[Constant.Order.OrderCode], BuildMailContent());
                    this.Page.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup();</script>");

                }
                else
                {
                    this.Page.Response.Write("<script type='text/javascript'>window.alert('Please insert some Product first!');</script>");
                }
            }
        }

        private int addNewCustomer(){
            SPList list = SPContext.Current.Web.Lists["Customer"];
            SPListItem newItem = list.Items.Add();
            newItem["Name"] = this.tfCustomerName.Value;
            newItem["Address"] = this.tfCustomerAddress.Value;
            newItem["Phone Number"] = this.tfCustomerPhone.Value;
            newItem["Tax Code"] = this.tfCustomerTaxCode.Value;
            newItem.Update();
            return newItem.ID;
        }

        protected void btGenerateCustomerInfo_Click(object sender, EventArgs e)
        {
            SPFieldLookupValue value = this.lfpcCustomer.Value as SPFieldLookupValue;
            if (value != null)
            {
                SPListItem item = SPContext.Current.Web.Lists["Customer"].GetItemById(value.LookupId);
                this.tfCustomerName.Text = item["Name"].ToString();
                this.tfCustomerAddress.Text = item["Address"].ToString();
                this.tfCustomerPhone.Text = item["Phone Number"].ToString();
            }
        }

        private void ShowHideControlInForm()
        {
            if (SPContext.Current.FormContext.FormMode == SPControlMode.Edit)
            {
                if (string.Equals(hdfStatus.Value, "Initiating"))
                {
                    if (web.IsCurrentUserMemberOfGroup(web.Groups["Sale Department"].ID))
                    {
                        this.btSendOrder.Visible = true;
                    }
                    else
                    {
                        this.tfOrderCode.ControlMode = SPControlMode.Display;
                        this.dtfExpectedDate.ControlMode = SPControlMode.Display;
                        this.tfCustomerName.ControlMode = SPControlMode.Display;
                        this.tfCustomerAddress.ControlMode = SPControlMode.Display;
                        this.tfCustomerPhone.ControlMode = SPControlMode.Display;
                        this.tfCustomerTaxCode.ControlMode = SPControlMode.Display;
                    }
                    this.txtComment.ControlMode = SPControlMode.Display;

                    
                    return;
                }
                else if (string.Equals(hdfStatus.Value, "Pending"))
                {
                    if (web.IsCurrentUserMemberOfGroup(web.Groups["Sale Manager"].ID))
                    {
                        this.btBackInfo.Visible = true;
                        this.btSendToDirector.Visible = true;
                        this.btReject.Visible = true;
                        this.txtComment.ControlMode = SPControlMode.Edit;
                    }
                    else
                    {
                        this.txtComment.ControlMode = SPControlMode.Display;
                    }
                    this.txtDirectorComment.ControlMode = SPControlMode.Display;
                    this.txtInventoryComment.ControlMode = SPControlMode.Display;

                }
                else if (string.Equals(hdfStatus.Value, "Director Review"))
                {
                    if (web.IsCurrentUserMemberOfGroup(web.Groups["Director"].ID))
                    {
                        this.btApprove.Visible = true;
                        this.btDirectorReject.Visible = true;
                        this.txtDirectorComment.ControlMode = SPControlMode.Edit;
                    }
                    else
                    {
                        this.txtDirectorComment.ControlMode = SPControlMode.Display;
                    }
                    this.txtComment.ControlMode = SPControlMode.Display;
                    this.txtInventoryComment.ControlMode = SPControlMode.Display;
                }
                else if ((string.Equals(hdfStatus.Value, "Approved") || string.Equals(hdfStatus.Value, "On Hold")))
                {
                    if (web.IsCurrentUserMemberOfGroup(web.Groups["Inventory Manager"].ID))
                    {
                        this.btFinish.Visible = true;
                        this.txtInventoryComment.ControlMode = SPControlMode.Edit;
                    }
                    else
                    {
                        this.txtInventoryComment.ControlMode = SPControlMode.Display;
                    }

                    this.txtComment.ControlMode = SPControlMode.Display;
                    this.txtDirectorComment.ControlMode = SPControlMode.Display;
                }
                else if (string.Equals(hdfStatus.Value, "Finished") || string.Equals(hdfStatus.Value, "Rejected"))
                {
                    this.txtComment.ControlMode = SPControlMode.Display;
                    this.txtDirectorComment.ControlMode = SPControlMode.Display;
                    this.txtInventoryComment.ControlMode = SPControlMode.Display;
                }
                tfOrderCode.ControlMode = SPControlMode.Display;
                dtfExpectedDate.ControlMode = SPControlMode.Display;
                tfCustomerName.ControlMode = SPControlMode.Display;
                tfCustomerAddress.ControlMode = SPControlMode.Display;
                tfCustomerPhone.ControlMode = SPControlMode.Display;
                tfCustomerTaxCode.ControlMode = SPControlMode.Display;

            }
            else if (string.Equals(hdfStatus.Value, "Initiating") && SPContext.Current.FormContext.FormMode != SPControlMode.Display && web.IsCurrentUserMemberOfGroup(web.Groups["Sale Department"].ID))
            {
                this.btSendOrder.Visible = true;
                return;
            }
            else if (SPContext.Current.FormContext.FormMode == SPControlMode.Display && string.Equals(hdfStatus.Value, "Finished") && (web.IsCurrentUserMemberOfGroup(web.Groups[Constant.Groups.SalesDepartment].ID) || web.IsCurrentUserMemberOfGroup(web.Groups[Constant.Groups.SalesManager].ID)))
            {
                this.btPrintOrder.Visible = true;
            }
        }

        private string BuildMailContent()
        {
            string content = string.Empty;

            string path = Request.Url.Authority + "/Lists/Order/DispForm.aspx?ID=" + SPContext.Current.Item["ID"].ToString();

            content += "<b>Order                :&nbsp;</b>" + item[Common.Constant.Order.OrderCode] + "<br/>";
            content += "<b>Expected Date        :&nbsp;</b>" + item[Common.Constant.Order.ExpectedDate] + "<br/>";
            content += "<b>Created Date         :&nbsp;</b>" + item[Common.Constant.Order.Created] + "<br/>";
            content += "<b>Customer Name        :&nbsp;</b>" + item[Common.Constant.Order.CustomerName] + "<br/>";
            content += "<b>Customer Address     :&nbsp;</b>" + item[Common.Constant.Order.CustomerAddress] + "<br/>";
            content += "<b>Customer Phone Number:&nbsp;</b>" + item[Common.Constant.Order.CustomerPhone] + "<br/>";
            content += "<b>Customer Tax Code    :&nbsp;</b>" + item[Common.Constant.Order.TaxCode] + "<br/><hr/>";
                        

            switch (hdfStatus.Value)
            {
                case "Initiating":
                    {
                        break;
                    }
                case "Pending":
                    {
                        SPUser saleManager = getUser(Common.Constant.Order.SaleManager);
                        content += "<b>Sale Manager         :&nbsp;</b>" + saleManager.Name + "<br/>";
                        content += "<b>Date                 :&nbsp;</b>" + item[Common.Constant.Order.ProcessedDate] + "<br/>";
                        content += "<b>Comment              :&nbsp;</b>" + item[Common.Constant.Order.SalesManagerComment] + "<br/>";
                        break;
                    }
                case "Director Review":
                    {
                        SPUser director = getUser(Common.Constant.Order.Director);
                        content += "<b>Director             :&nbsp;</b>" + director.Name + "<br/>";
                        content += "<b>Date                 :&nbsp;</b>" + item[Common.Constant.Order.ApproveDate] + "<br/>";
                        content += "<b>Comment              :&nbsp;</b>" + item[Common.Constant.Order.DirectorComment] + "<br/>";
                        break;
                    }
                case "On Hold" :
                case "Approved":
                    {
                        SPUser inventoryManager = getUser(Common.Constant.Order.InventoryManager);
                        content += "<b>Inventory Manager    :&nbsp;</b>" + inventoryManager.Name + "<br/>";
                        content += "<b>Date                 :&nbsp;</b>" + item[Common.Constant.Order.FinishedDate] + "<br/>";
                        content += "<b>Comment              :&nbsp;</b>" + item[Common.Constant.Order.InventoryComment] + "<br/>";
                        break;
                    }

            }

            content +="<hr/>"+path + "<br/>";
            return content;
        }

        protected void btPrintOrder_Click(object sender, EventArgs e)
        {
            Common.Common.GenerateOrderPrint(item, Session[this.hdfSessionName.Value] as DataTable);
        }
    }
}
